package team.seekers.skill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.skill.entity.dao.UserSection;
import team.seekers.skill.handler.exceptionHandler.ZzuException;
import team.seekers.skill.mapper.UserSectionMapper;
import team.seekers.skill.service.UserSectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-01-27
 */
@Service
public class UserSectionServiceImpl extends ServiceImpl<UserSectionMapper, UserSection> implements UserSectionService {

    @Override
    public Integer getStatus(String userId, String sectionId) {
        QueryWrapper<UserSection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("section_id",sectionId);
        List<UserSection> list = list(wrapper);
        if(list.size() > 1){
            throw new ZzuException("操作失败，请稍后重新尝试",3000);
        }else {
            UserSection userSection = getOne(wrapper);
            if (userSection == null)
                return 0;
            else
                return userSection.getStatus();
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean removeBySectionId(String sectionId){
        QueryWrapper<UserSection> wrapper = new QueryWrapper<>();
        wrapper.eq("section_id",sectionId);
        boolean remove = remove(wrapper);
        return remove;
    }
}
